//
//  OutMoneyVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/8/6.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "OutMoneyVC.h"
#import "TransferMoneyCell.h"
#import "AddCardVC.h"

#import "ZZPSDInputView.h"
#import "ZZActionSheet.h"
@interface OutMoneyVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation OutMoneyVC
{
    UITableView *_tableView;
    UITextField *_sumTextField;
    UITableView *_cardTableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if (self.isIntoMoney == YES) {
        self.title = @"充值";
    }else{
        self.title = @"转出到银行卡";
    }
    [self initTableView];
    [self initView];
    //设置多余的分割线不显示
    [self setExtraCellLineHidden:_tableView];

}
- (void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _tableView.backgroundColor = LighterGrayColor;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    //
    _cardTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, KScreenHeight, KScreenWidth, 200)];
    _cardTableView.delegate = self;
    _cardTableView.dataSource = self;
    [self.view addSubview:_cardTableView];
}
- (void)initView{
    UILabel *noticeLb = [[UILabel alloc]initWithFrame:CGRectMake(60, 55, 200, 30)];
    if (self.isIntoMoney == YES) {
        noticeLb.text = @"*立即到账";
    }else{
        noticeLb.text = @"*两小时内到账";
    }
    noticeLb.textColor = Orange_Color;
    noticeLb.font = [UIFont systemFontOfSize:12];
    [_tableView addSubview:noticeLb];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 90, KScreenWidth, 35)];
    bgView.backgroundColor = [UIColor whiteColor];
    [_tableView addSubview:bgView];
    UILabel *sumLb = [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 50, 35)];
    sumLb.text = @"金额";
    [bgView addSubview:sumLb];
    _sumTextField = [[UITextField alloc]initWithFrame:CGRectMake(110, 0, 150, 35)];
    if (self.isIntoMoney == YES) {
        _sumTextField.placeholder = @"请输入充值金额";
    }else{
        _sumTextField.placeholder = @"最多转出1234.5元";
    }
    
    [bgView addSubview:_sumTextField];
    
    UIButton *doneBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 150, KScreenWidth, 35)];
    doneBtn.backgroundColor = [UIColor whiteColor];
    if (self.isIntoMoney == YES) {
        [doneBtn setTitle:@"确认充值" forState:UIControlStateNormal];
    }else{
        [doneBtn setTitle:@"确认转出" forState:UIControlStateNormal];

    }
    [doneBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    doneBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [doneBtn addTarget:self action:@selector(doneBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_tableView addSubview:doneBtn];
}
#pragma - mark UITableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _cardTableView) {
        return 3;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TransferMoneyCell *cell = [[NSBundle mainBundle]loadNibNamed:@"TransferMoneyCell" owner:nil options:nil][0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 2) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"使用新卡付款";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        return cell;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _tableView && indexPath.row == 0) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelegate:self];
        [_cardTableView setFrame:CGRectMake(0,KScreenHeight - 250, KScreenWidth, 200)];
        [UIView commitAnimations];
        _tableView.backgroundColor = LighterGrayColor;
        // ╳
        UIButton *exitButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
        [exitButton setTitle:@"╳" forState:UIControlStateNormal];
        [exitButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [exitButton addTarget:self action:@selector(exit) forControlEvents:UIControlEventTouchUpInside];
        [_cardTableView addSubview:exitButton];
    }
    if (tableView == _cardTableView && indexPath.row ==2) {
        AddCardVC *vc = [[AddCardVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        _tableView.alpha = 1;
        UIView *subviews  = [self.view viewWithTag:100];
        [subviews removeFromSuperview];
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 58;
}
//设置TableViewCell多余的分割线不显示
-(void)setExtraCellLineHidden: (UITableView *)tableView{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}
//Btn点击事件
- (void)doneBtnAction{
    [_sumTextField resignFirstResponder];
    //弹出键盘输入密码
    ZZActionSheet *sheet        = [[ZZActionSheet alloc] init];
    sheet.dismissWhenTouchBlank = YES;
    
    ZZPSDInputView *psdView     = [ZZPSDInputView psdInputView];
    psdView.backgroundColor     = [UIColor whiteColor];
    __weak __typeof(sheet) weakSheet = sheet;
    psdView.cancelHandler = ^() {
        [weakSheet dismiss];
    };
    psdView.finishHandler = ^(NSString *passStr) {
        [weakSheet dismissWithCompletion:^{
            //此处判断密码是否正确
            NSLog(@"完成: %@", passStr);
        }];
    };

    sheet.contentView = psdView;
    [sheet show];
}
- (void)exit{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [_cardTableView setFrame:CGRectMake(0, KScreenHeight, KScreenWidth, 200)];
    [UIView commitAnimations];
    _tableView.backgroundColor = LighterGrayColor;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
